/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/***************************************************************************
 * Name: function_wrapper.h
 *
 * Purpose: define runtime helper API for wrapper function as an object
 *
 * Developer:
 *   wen.gu , 2020-04-26
 *
 * TODO:
 *
 ***************************************************************************/

#ifndef __ARA_COM_RUNTIME_FUNCTION_WRAPPER_H__
#define __ARA_COM_RUNTIME_FUNCTION_WRAPPER_H__
/******************************************************************************
 **    INCLUDES
 ******************************************************************************/

#include "ara/com/runtime/runtime_utils.h"


/******************************************************************************
 **    MACROS
 ******************************************************************************/


/******************************************************************************
 **    TYPE DEFINITIONS
 ******************************************************************************/
namespace ara
{
namespace com
{
namespace runtime
{

/******************************************************************************
 **    CLASSES/FUNCTIONS DEFINITIONS
 ******************************************************************************/
template<typename R, typename... Args>
class Function
{
public:
    virtual R operator()(Args...) = 0;
};

/** function wrapper for the member function of class */
template<class C, typename R, typename... Args>
class FunctionOfMem : public Function<R, Args...>
{
public:
    typedef R(C::* MemFuncType)(Args...);

    MemFuncType mFunc;
    C* mObj;

    FunctionOfMem(MemFuncType func, C* obj)
    {
        mFunc = func;
        mObj = obj;
    }

    virtual R operator()(Args... args)
    {
        return (mObj->*mFunc)(args...);
    }
};


/** function wrapper for normal function */
template<typename R, typename... Args>
class FunctionOfPtr : public Function<R, Args...>
{
public:
    typedef R(*PtrFuncType)(Args...);
    PtrFuncType mFunc;

    FunctionOfPtr(PtrFuncType func)
    {
        mFunc = func;
    }

    virtual R operator()(Args... args)
    {
        return (*mFunc)(args...);
    }
};


/** bind function to function object */
template<typename R, typename... Args>
Function<R, Args...> Bind(R(*func_ptr)(Args...))
{
    return FunctionOfPtr<R, Args...>(func_ptr);
}

template<class C, typename R, typename... Args>
Function<R, Args...> Bind(R(C::* mem_func)(Args...), C* obj)
{
    return FunctionOfMem<C, R, Args...>(mem_func, obj);
}


/**for expand tuple as function parameters */
template <typename F, typename T, size_t ...I>
inline auto ApplyImpl(F& f, const T& t, IndexSequence<I...>) -> decltype(f(std::get<I>(t)...))
{
    return f(std::get<I>(t)...);
}

template <typename R, typename T, typename... Args>
inline R Apply(R(*funcPtr)(Args...), const T& t)
{
    return ApplyImpl(*funcPtr, t, IndexSequenceFor<Args...>());
}

template <class C, typename R, typename T, typename... Args>
inline R Apply(C* c, R(C::* memFunc)(Args...), const T& t)
{
    auto func = Bind(memFunc, c);
    return ApplyImpl(func, t, IndexSequenceFor<Args...>());
}



} /** namespace runtime */
} /** namespace com */
} /** namespace ara */

#endif /** !__ARA_COM_RUNTIME_FUNCTION_WRAPPER_H__ */

